import java.io.*;
import java.util.*;

public class 栈和排序 {
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    public static void main(String[] args) throws IOException {
        int n = in.nextInt();
        // 找到开始的最大值
        int max = Integer.MIN_VALUE;
        // 存储入栈的排列
        int[] arr = new int[n];
        // 大的出栈, 拿一个数组来存储
        int[] maxs = new int[n];
        // 大数组的指针
        int cnt1 = 0;
        // 初始化并找到最大值
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
            max = Math.max(max,arr[i]);
        }
        // 将最大值存入数组
        maxs[cnt1++] = max;
        // 入栈的数组, 模拟一个栈操作
        int[] stack = new int[n];
        int cnt2 = 0;
        // 开始遍历所有元素
        for (int i = 0; i < n; i++) {
            // 如果找到最大值
            if (max == arr[i]) {
                // 发现下一个元素越界, 说明没有元素了, 直接 break
                int cur = i + 1;
                if (cur >= n) {
                    break;
                }
                // 后面还有元素, 找到最大值
                max = findMax(arr,cur);
                // 前一个元素是有值的, 并且要大于该元素后面找到的最大元素
                while (cnt2 - 1 >= 0 && stack[cnt2 - 1] > max) {
                    // 放到最大数组里
                    maxs[cnt1++] = stack[--cnt2];
                }
                // 将之后找到的最大元素放到最大数组里
                maxs[cnt1++] = max;
            } else {
                // 入栈
                stack[cnt2++] = arr[i];
            }
        }
        // 打印结果
        for (int i = 0; i < cnt1; i++) {
            System.out.print(maxs[i] + " ");
        }
        for (int i = cnt2 - 1; i >= 0; i--) {
            if (i == 0) {
                System.out.print(stack[i]);
            } else {
                System.out.print(stack[i] + " ");
            }
        }
        out.close();
    }

    private static int findMax(int[] arr, int cur) {
        int max = arr[cur];
        for (int i = cur + 1; i < arr.length; i++) {
            max = Math.max(max,arr[i]);
        }
        return max;
    }

    public static class Read // 自定义快速读入
    {
        StringTokenizer st = new StringTokenizer("");
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String next() throws IOException
        {
            while(!st.hasMoreTokens())
            {
                st = new StringTokenizer(bf.readLine());
            }
            return st.nextToken();
        }

        String nextLine() throws IOException
        {
            return bf.readLine();
        }

        int nextInt() throws IOException
        {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException
        {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException
        {
            return Double.parseDouble(next());
        }
    }
}
